package com.zhq.blog.core.domain;

import javax.annotation.PostConstruct;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhq.blog.core.domain.admin.AdminControllerCategoryRepository;
import com.zhq.blog.core.domain.admin.AdminControllerDescriptionRepository;
import com.zhq.blog.core.domain.admin.OperatorRepository;
import com.zhq.blog.core.domain.admin.WorkLogRepository;
import com.zhq.blog.core.domain.game.GamePeriodRepository;
import com.zhq.blog.core.domain.game.GameRepository;
import com.zhq.blog.core.domain.game.support.AutoBetDetailRepository;
import com.zhq.blog.core.domain.game.support.AutoBetRepository;
import com.zhq.blog.core.domain.game.support.CathecticModelDetailRepository;
import com.zhq.blog.core.domain.game.support.CathecticModelRepository;
import com.zhq.blog.core.domain.game.support.CathecticRepository;
import com.zhq.blog.core.domain.game.support.OddsRepository;
import com.zhq.blog.core.domain.game.support.PermutationBetDetailRepository;
import com.zhq.blog.core.domain.game.support.PermutationBetRepository;
import com.zhq.blog.core.domain.game.support.WinnerRecordRepository;
import com.zhq.blog.core.domain.money.BusinessManRepository;
import com.zhq.blog.core.domain.money.CardRecordRepository;
import com.zhq.blog.core.domain.money.CardRepository;
import com.zhq.blog.core.domain.money.CreateCardRepository;
import com.zhq.blog.core.domain.other.DetailsOfFundsRepository;
import com.zhq.blog.core.domain.other.InternalMessageRepository;
import com.zhq.blog.core.domain.product.CategoryRepository;
import com.zhq.blog.core.domain.product.NoteRepository;
import com.zhq.blog.core.domain.product.PrizeExplainRepository;
import com.zhq.blog.core.domain.product.PrizeRepository;
import com.zhq.blog.core.domain.support.EnumAwareConvertUtilsBean;
import com.zhq.blog.core.domain.system.HelpRepository;
import com.zhq.blog.core.domain.system.HoldIdRepository;
import com.zhq.blog.core.domain.system.InformationRepository;
import com.zhq.blog.core.domain.system.NoticeRepository;
import com.zhq.blog.core.domain.user.SaleRepository;
import com.zhq.blog.core.domain.user.UserRankRepository;
import com.zhq.blog.core.domain.user.UserRepository;

@Component
public class Context {
	
	public enum Environment{
		DEVELOP("DEVELOP"),TEST("TEST"),PRODUCTION("PRODUCTION");
		public String name;
		
		private Environment(String name){
			this.name = name;
		}
	}
	

	public static final String SYSTEM_ENV_FLAG ="HUALALA_ENV";
	public Environment environment = Environment.DEVELOP;
	
	public static Context current;
	
	@Autowired
    private OperatorRepository operatorRepository;
	
	@Autowired
	private AdminControllerCategoryRepository adminControllerCategoryRepository;
	
	@Autowired
	private AdminControllerDescriptionRepository adminControllerDescriptionRepository;
	
	@Autowired
    private WorkLogRepository workLogRepository;
	
	@Autowired
	private GameRepository gameRepository;
	
	@Autowired
	private GamePeriodRepository gamePeriodRepository;
	
	@Autowired
	private UserRepository userRepository;
	
	@Autowired
	private CategoryRepository categoryRepository;
	
	@Autowired
	private UserRankRepository userRankRepository;
	
	@Autowired
	private NoteRepository noteRepository;
	
	@Autowired
	private BusinessManRepository businessManRepository;
	
	@Autowired
	private CardRepository cardRepository;
	
	@Autowired
	private CreateCardRepository createCardRepository;
	
	@Autowired
	private CardRecordRepository cardRecordRepository;
	
	@Autowired
	private PrizeRepository prizeRepository;
	
	@Autowired
	private PrizeExplainRepository prizeExplainRepository;
	
	@Autowired
	private SaleRepository saleRepository;
	
	@Autowired
	private CathecticRepository cathecticRepository;
	
	@Autowired
	private InternalMessageRepository internalMessageRepository;
	
	@Autowired
	private OddsRepository oddsRepository;
	
	@Autowired
	private DetailsOfFundsRepository detailsOfFundsRepository;
	
	@Autowired
	private AutoBetRepository autoBetRepository;
	
	@Autowired
	private AutoBetDetailRepository autoBetDetailRepository;
	
	@Autowired
	private CathecticModelRepository cathecticModelRepository;
	
	@Autowired
	private CathecticModelDetailRepository cathecticModelDetailRepository;

	@Autowired
	private PermutationBetRepository permutationBetRepository;
	
	@Autowired
	private PermutationBetDetailRepository permutationBetDetailRepository;
	
	@Autowired
	private InformationRepository informationRepository;
	
	@Autowired
	private HelpRepository helpRepository;
	
	@Autowired
	private NoticeRepository noticeRepository;
	
	@Autowired
	private HoldIdRepository holdIdRepository;
	
	@Autowired
	private WinnerRecordRepository winnerRecordRepository;
	
	public WinnerRecordRepository getWinnerRecordRepository() {
		return winnerRecordRepository;
	}

	public void setWinnerRecordRepository(
			WinnerRecordRepository winnerRecordRepository) {
		this.winnerRecordRepository = winnerRecordRepository;
	}

	public HoldIdRepository getHoldIdRepository() {
		return holdIdRepository;
	}

	public void setHoldIdRepository(HoldIdRepository holdIdRepository) {
		this.holdIdRepository = holdIdRepository;
	}

	public HelpRepository getHelpRepository() {
		return helpRepository;
	}

	public void setHelpRepository(HelpRepository helpRepository) {
		this.helpRepository = helpRepository;
	}

	public NoticeRepository getNoticeRepository() {
		return noticeRepository;
	}

	public void setNoticeRepository(NoticeRepository noticeRepository) {
		this.noticeRepository = noticeRepository;
	}

	public InformationRepository getInformationRepository() {
		return informationRepository;
	}

	public void setInformationRepository(InformationRepository informationRepository) {
		this.informationRepository = informationRepository;
	}

	public PermutationBetRepository getPermutationBetRepository() {
		return permutationBetRepository;
	}

	public void setPermutationBetRepository(
			PermutationBetRepository permutationBetRepository) {
		this.permutationBetRepository = permutationBetRepository;
	}

	public CardRepository getCardRepository() {
		return cardRepository;
	}

	public void setCardRepository(CardRepository cardRepository) {
		this.cardRepository = cardRepository;
	}

	public CreateCardRepository getCreateCardRepository() {
		return createCardRepository;
	}

	public void setCreateCardRepository(CreateCardRepository createCardRepository) {
		this.createCardRepository = createCardRepository;
	}

	public CardRecordRepository getCardRecordRepository() {
		return cardRecordRepository;
	}

	public void setCardRecordRepository(CardRecordRepository cardRecordRepository) {
		this.cardRecordRepository = cardRecordRepository;
	}
	

	public PrizeExplainRepository getPrizeExplainRepository() {
		return prizeExplainRepository;
	}

	public void setPrizeExplainRepository(
			PrizeExplainRepository prizeExplainRepository) {
		this.prizeExplainRepository = prizeExplainRepository;
	}

	public PermutationBetDetailRepository getPermutationBetDetailRepository() {
		return permutationBetDetailRepository;
	}

	public void setPermutationBetDetailRepository(
			PermutationBetDetailRepository permutationBetDetailRepository) {
		this.permutationBetDetailRepository = permutationBetDetailRepository;
	}

	public CathecticModelRepository getCathecticModelRepository() {
		return cathecticModelRepository;
	}

	public void setCathecticModelRepository(
			CathecticModelRepository cathecticModelRepository) {
		this.cathecticModelRepository = cathecticModelRepository;
	}

	public CathecticModelDetailRepository getCathecticModelDetailRepository() {
		return cathecticModelDetailRepository;
	}

	public void setCathecticModelDetailRepository(
			CathecticModelDetailRepository cathecticModelDetailRepository) {
		this.cathecticModelDetailRepository = cathecticModelDetailRepository;
	}

	public AutoBetRepository getAutoBetRepository() {
		return autoBetRepository;
	}

	public void setAutoBetRepository(AutoBetRepository autoBetRepository) {
		this.autoBetRepository = autoBetRepository;
	}

	public AutoBetDetailRepository getAutoBetDetailRepository() {
		return autoBetDetailRepository;
	}

	public void setAutoBetDetailRepository(
			AutoBetDetailRepository autoBetDetailRepository) {
		this.autoBetDetailRepository = autoBetDetailRepository;
	}

	public DetailsOfFundsRepository getDetailsOfFundsRepository() {
		return detailsOfFundsRepository;
	}

	public void setDetailsOfFundsRepository(
			DetailsOfFundsRepository detailsOfFundsRepository) {
		this.detailsOfFundsRepository = detailsOfFundsRepository;
	}

	public GamePeriodRepository getGamePeriodRepository() {
		return gamePeriodRepository;
	}

	public void setGamePeriodRepository(GamePeriodRepository gamePeriodRepository) {
		this.gamePeriodRepository = gamePeriodRepository;
	}

	public OddsRepository getOddsRepository() {
		return oddsRepository;
	}

	public void setOddsRepository(OddsRepository oddsRepository) {
		this.oddsRepository = oddsRepository;
	}

	public BusinessManRepository getBusinessManRepository() {
		return businessManRepository;
	}

	public PrizeRepository getPrizeRepository() {
		return prizeRepository;
	}

	public void setPrizeRepository(PrizeRepository prizeRepository) {
		this.prizeRepository = prizeRepository;
	}

	public SaleRepository getSaleRepository() {
		return saleRepository;
	}

	public void setSaleRepository(SaleRepository saleRepository) {
		this.saleRepository = saleRepository;
	}

	public void setBusinessManRepository(BusinessManRepository businessManRepository) {
		this.businessManRepository = businessManRepository;
	}

	public UserRankRepository getUserRankRepository() {
		return userRankRepository;
	}

	public void setUserRankRepository(UserRankRepository userRankRepository) {
		this.userRankRepository = userRankRepository;
	}

	public UserRepository getUserRepository() {
		return userRepository;
	}


	public NoteRepository getNoteRepository() {
		return noteRepository;
	}

	public void setNoteRepository(NoteRepository noteRepository) {
		this.noteRepository = noteRepository;
	}

	public CategoryRepository getCategoryRepository() {
		return categoryRepository;
	}

	public void setCategoryRepository(CategoryRepository categoryRepository) {
		this.categoryRepository = categoryRepository;
	}

	public CathecticRepository getCathecticRepository() {
		return cathecticRepository;
	}

	public void setCathecticRepository(CathecticRepository cathecticRepository) {
		this.cathecticRepository = cathecticRepository;
	}

	public void setUserRepository(UserRepository userRepository) {
		this.userRepository = userRepository;
	}

	public OperatorRepository getOperatorRepository() {
		return operatorRepository;
	}

	public void setOperatorRepository(OperatorRepository operatorRepository) {
		this.operatorRepository = operatorRepository;
	}

	public AdminControllerCategoryRepository getAdminControllerCategoryRepository() {
		return adminControllerCategoryRepository;
	}

	public void setAdminControllerCategoryRepository(
			AdminControllerCategoryRepository adminControllerCategoryRepository) {
		this.adminControllerCategoryRepository = adminControllerCategoryRepository;
	}

	public AdminControllerDescriptionRepository getAdminControllerDescriptionRepository() {
		return adminControllerDescriptionRepository;
	}

	public void setAdminControllerDescriptionRepository(
			AdminControllerDescriptionRepository adminControllerDescriptionRepository) {
		this.adminControllerDescriptionRepository = adminControllerDescriptionRepository;
	}

	public InternalMessageRepository getInternalMessageRepository() {
		return internalMessageRepository;
	}

	public void setInternalMessageRepository(
			InternalMessageRepository internalMessageRepository) {
		this.internalMessageRepository = internalMessageRepository;
	}

	public WorkLogRepository getWorkLogRepository() {
		return workLogRepository;
	}

	public void setWorkLogRepository(WorkLogRepository workLogRepository) {
		this.workLogRepository = workLogRepository;
	}
	
	public GameRepository getGameRepository() {
		return gameRepository;
	}

	public void setGameRepository(GameRepository gameRepository) {
		this.gameRepository = gameRepository;
	}

	/**
	 * 全局初始化，包括内容
	 * 1、初始化环境变量，分为开发环境、测试环境和生产环境，使用环境变量中LUNA_ENV的标识区分，如设置为LUNA_ENV=TEST，则为测试环境
	 * 2、设置apache commons中BeanUtils转换枚举类型的方法
	 * 3、初始化数据
	 */
	@PostConstruct
	public void init(){
		Context.current = this;
		Context.initEnvironment(this);
		BeanUtilsBean.setInstance(new BeanUtilsBean(new EnumAwareConvertUtilsBean()));
		
		if(this.operatorRepository.count() == 0){
			new Seed(this).init();
		}
	}
	
	public static void initEnvironment(Context context){
		if(System.getenv().containsKey(SYSTEM_ENV_FLAG)){
			if(Environment.DEVELOP.name.equals(System.getenv().get(SYSTEM_ENV_FLAG))){
				context.environment = Environment.DEVELOP;
			}else if(Environment.TEST.name.equals(System.getenv().get(SYSTEM_ENV_FLAG))){
				context.environment = Environment.TEST;
			}else if(Environment.PRODUCTION.name.equals(System.getenv().get(SYSTEM_ENV_FLAG))){
				context.environment = Environment.PRODUCTION;
			}			
		}		
	}
}
